<?php
namespace Admin\Controller;
use Think\Controller;
use Org\Net\Http;
use Think\Image;
class UploadController extends Controller {
    public function index(){
    	var_dump($_SERVER);
//     	$this->display();
    }
    public function receive(){
    	vendor("Upload.receive");
    	header("Content-type: text/html; charset=utf-8");
    	$Handle = new \UploadReceive();
    	$data = $Handle->receive($_FILES['upload'],'Public/Upload/');
    	//echo json_encode($data,JSON_UNESCAPED_UNICODE);
    	$this->ajaxReturn($data);
    }
    public function uploadImg(){
    	$file = $_FILES['upload'];
    	$image = new Image();
    	$path = "Public/Upload/upload/";
//     	$file_name = $file['name'];
    	$path_info = pathinfo($file['name']);
    	$file_extension = $path_info["extension"];
    	$file_name = uniqid().".".$file_extension;
    	$path_name = $path.$file_name;
    	$image->open($file['tmp_name']);
    	$result = $image->save($path_name);
    	if($result){
    		$data['status'] = 1;
    		$data['info'] = '成功';
    		$data['data'] = array(
    				"path"  => 'Http://'.$_SERVER['HTTP_HOST'].'/'.__ROOT__.'/'.$path_name,
    				"name"  => $file_name,
    				"width" => $image->width(),
    				"height"=> $image->height()
    		);
    	}else{
    		$data['status'] = 0;
    		$data['info'] = '失败';
    	}
    	$this->ajaxReturn($data);
    }
    public function cutImg(){
    	$image = new Image();
    	$path = "Public/Upload/upload/";
    	$file_name = $_POST['name'];
    	$path_name = $path.$file_name;
    	$image->open($path_name);
    	
    	$path = "Public/Upload/cut/";
    	$path_name = $path.$file_name;
    	
    	$x = $_POST['offsetLeft'];
    	$y = $_POST['offsetTop'];
    	$width = $_POST['width'];
    	$height = $_POST['height'];
    	$result = $image->crop($width, $height,$x,$y)->save($path_name);
    	if($result){
    		$data['status'] = 1;
    		$data['info'] = '成功';
    		$data['path'] = 'Http://'.$_SERVER['HTTP_HOST'].'/'.__ROOT__.'/'.$path_name;
    		$data['width'] = $image->width();
    		$data['height'] = $image->height();
    	}else{
    		$data['status'] = 0;
    		$data['info'] = '失败';
    	}
    	$this->ajaxReturn($data,'json');
    	
    }
}